package com.wrh.springboot.restwrap;

import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;

/**
 * @author wrh
 */
public class RestResponse {

    public static <T> RestResult<T> makRsp(@NonNull Integer code, @Nullable Long cost, @NonNull String msg, @Nullable T data) {
        return new RestResult<T>(code, cost, msg, data);
    }


    public static <T> RestResult<T> ok(@Nullable Long cost, @NonNull String msg, @Nullable T data) {
        return makRsp(RestCodeEnum.SUCCESS.getCode(), cost, msg, data);
    }

    public static <T> RestResult<T> ok(@Nullable Long cost, @Nullable T data) {
        return ok(cost, RestCodeEnum.SUCCESS.getDefaultMsg(), data);
    }

    public static <T> RestResult<T> ok(@Nullable Long cost) {
        return ok(cost);
    }

    public static <T> RestResult<T> ok() {
        return ok(null);
    }


    public static <T> RestResult<T> fail(@NonNull String msg) {
        return makRsp(RestCodeEnum.FAIL.getCode(), null, msg, null);
    }

    public static <T> RestResult<T> fail() {
        return fail(RestCodeEnum.FAIL.getDefaultMsg());
    }


    public static <T> RestResult<T> unAuth(@NonNull String msg) {
        return makRsp(RestCodeEnum.UNAUTHORIZED.getCode(), null, msg, null);
    }

    public static <T> RestResult<T> unAuth() {
        return unAuth(RestCodeEnum.UNAUTHORIZED.getDefaultMsg());
    }


    public static <T> RestResult<T> notFound(@NonNull String msg) {
        return makRsp(RestCodeEnum.NOT_FOUND.getCode(), null, msg, null);
    }

    public static <T> RestResult<T> notFound() {
        return notFound(RestCodeEnum.NOT_FOUND.getDefaultMsg());
    }


    public static <T> RestResult<T> err(@NonNull String msg) {
        return makRsp(RestCodeEnum.INTERNAL_SERVER_ERROR.getCode(), null, msg, null);
    }

    public static <T> RestResult<T> err() {
        return err(RestCodeEnum.INTERNAL_SERVER_ERROR.getDefaultMsg());
    }

    public static <T> RestResult<T> err(Exception e) {
        return err(e.getMessage());
    }


}
